
/*
灯：设备上，由一个或多个不同Led组成的，状态指示
*/

#include <stdint.h>
#include "BitDefine.h"
#include "Bsp_Led.h"
#include "LedId.h"
#include "Light.h"

#include "Indecator.h"

static const IndecatorSetting_T lightPermanent = {
	.states[State_Off] = {Color_Black, 0},
	.states[State_On] = {Color_Red, 0xFFFF},
	.duration = 0,
};

static void Indecator_Light_Set(uint32_t id, uint32_t color){
	if(color != Color_Black){
		//Color 减去Black，得到对应颜色的Led的Id
		uint32_t c = color - 1;
		BSP_Led_MultSet(id, Bit(c));
	}
	else{
		BSP_Led_MultSet(id, 0);
	}
}

	
static Indecator_T lightIndecator = {
	.obj = {
		.action = Indecator_Light_Set,				//灯设置函数
		.objId = Bit(LedId_Green)|Bit(LedId_Red),	//灯支持的颜色
	},
};

//灯初始化
void Light_Init(void){
	//常态红灯常亮
	//红灯为电源指示灯，初始化即点亮	
//	Indecator_Light_Set(Bit(LedId_Red), Color_Red);
	
	Indecator_Init();
//	Indecator_Attach(Inde_Light, &lightObj);	
	Indecator_Set(&lightIndecator, Setting_Permanent, &lightPermanent, 0);
	Indecator_Register(&lightIndecator);
}


//设置灯闪烁
void Light_FlashAsync(uint8_t id, uint8_t count, uint32_t onMs, uint8_t onColor, uint32_t offMs, uint8_t offColor){
	IndecatorSetting_T lightTemp = {
		.states[State_Off] = {offColor, offMs/10},
		.states[State_On] = {onColor, onMs/10},
		.duration = (onMs/10 + offMs/10)*count,
		.current = State_On,
	};

	Indecator_Set(&lightIndecator, Setting_Temp, 0, &lightTemp);
}

//设置灯的颜色
void Light_Set(uint8_t id, uint8_t color){
	Light_FlashAsync(0, 0, 1000, color, 0, 0);
}

//恢复常态
void Light_Permanent(uint8_t id){
	Indecator_Set(&lightIndecator, Setting_Permanent, 0, 0);
}

